<!--预览界面-->
<template>
  <Main :type="'ordinary'">
    <!-- <Title :title="'微信授权'" /> -->
    <el-steps
      id="stepsWX"
      :active="2"
      finish-status="success"
      style="padding:30px 150px;"
    >
      <el-step title="授权" icon=""></el-step>
      <el-step title="更新" icon=""></el-step>
      <el-step title="预览" icon=""></el-step>
      <el-step title="发布" icon=""></el-step>
    </el-steps>
    <div class="add">
      <el-input v-model="input" placeholder="请输入微信账号">
        <el-button slot="append" @click="add">添加体验者</el-button>
      </el-input>
    </div>
    <div class="list scrollbars">
      <div v-for="(item, index) in wechatId" :key="index" class="box">
        <el-tooltip
          class="item"
          effect="dark"
          :content="item.wechat_id"
          placement="top"
        >
          <div class="box-name">
            {{ item.wechat_id }}
          </div>
        </el-tooltip>
        <div class="box-del">
          <i class="el-icon-delete" @click="del(item)"></i>
        </div>
      </div>
    </div>
    <div v-if="shows" class="imgs">
      <img :src="qrcodeUrl" />
      <div class="font">扫描二维码</div>
    </div>
    <div v-else class="imgs">
      <img src="http://o6wndwjxn.qnssl.com/46a34201806301243402835.png" />
      <div class="font">暂无二维码</div>
    </div>
  </Main>
</template>

<script>
import {
  GETONE,
  AUTHORIZE_STATUS,
  EXPERLIST,
  PULLEXPER,
  EXPER
} from "./service";

export default {
  name: "RootBoard",
  data() {
    return {
      input: "",
      qrcodeUrl: "",
      mag: "",
      wechatId: "",
      dialogVisible: false,
      shows: true,
      draglist: []
    };
  },
  created() {
    this.init();
    this.getOne();
    this.experList();
  },
  methods: {
    async getOne() {
      await GETONE().then(({ code, data }) => {
        if (code === 200) {
          this.qrcodeUrl = this.$store.state.qiniu + data.qrcode_url;
          data.status === "noauthorize"
            ? (this.shows = false)
            : (this.shows = true);
        }
      });
    },
    async experList() {
      await EXPERLIST().then(({ code, data, msg }) => {
        if (code === 200) {
          this.wechatId = Array.prototype.slice.call(data);
        } else {
          this.$message.error(msg);
        }
      });
    },
    async init() {
      await AUTHORIZE_STATUS().then(res => {
        let { code, data } = res.data;
        if (code === 200) {
          if (data !== "authorized") {
            this.$message({
              message: "您还没有授权，现在进入授权界面",
              type: "success"
            });
            this.$router.push({
              path: "/wxchat",
              name: "wxchat"
            });
          }
        }
      });
    },
    async del(item) {
      await this.$confirm(
        `是否将微信账号为 ${item.wechat_id} 移出体验者`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          PULLEXPER({ wechatid: item.wechat_id }).then(({ code }) => {
            if (code === 200) {
              this.experList();
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {});
    },
    onSubmit() {
      this.dialogVisible = true;
    },
    async add() {
      if (!this.input) return this.$message.error("微信账号不能为空！");
      await this.$confirm(
        `是否将微信账号为 ${this.input} 添加到体验者`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          EXPER({ weichat: this.input }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.experList();
            } else {
              this.$message.error(msg);
            }
            this.input = "";
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.add {
  width: 30%;
  margin: 30px auto;
  text-align: center;
}
.list {
  width: 66%;
  max-height: 320px;
  margin: 40px auto;
  display: flex;
  flex-wrap: wrap;
  .box {
    width: 200px;
    height: 40px;
    line-height: 40px;
    background: #ebecf0;
    margin: 0 10px 10px 0;
    font-size: 14px;
    text-align: center;
    color: #666666;
    .box-name {
      float: left;
      width: 88%;
      padding-left: 10px;
      box-sizing: border-box;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .box-del {
      float: left;
      width: 12%;
    }
  }
}
.imgs {
  width: 20%;
  margin: 0 auto;
  text-align: center;
  img {
    width: 180px;
    height: 180px;
  }
  .font {
    margin-top: 10px;
  }
}
</style>
<style>
#stepsWX .el-step__head.is-success {
  color: #409eff;
  border-color: #409eff;
}
#stepsWX .el-step__title.is-success {
  color: #409eff !important;
}
</style>
